// compareTpuNodeSchedulingConfig diff suppresses for the default
// scheduling, i.e. if preemptible is false, the API may either return no
// schedulingConfig or an empty schedulingConfig.
func compareTpuNodeSchedulingConfig(k, old, new string, d *schema.ResourceData) bool {
	if k == "scheduling_config.0.preemptible" {
		return old == "" && new == "false"
	}
	if k == "scheduling_config.#" {
		o, n := d.GetChange("scheduling_config.0.preemptible")
		return o.(bool) == n.(bool)
	}
	return false
}

func tpuNodeCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {
	old, new := diff.GetChange("network")
	config := meta.(*Config)

	networkLinkRegex := regexp.MustCompile("projects/(.+)/global/networks/(.+)")

	var pid string

	if networkLinkRegex.MatchString(new.(string)) {
		parts := networkLinkRegex.FindStringSubmatch(new.(string))
		pid = parts[1]
	}

	project, err := config.NewResourceManagerClient(config.userAgent).Projects.Get(pid).Do()
	if err != nil {
		return fmt.Errorf("Failed to retrieve project, pid: %s, err: %s", pid, err)
	}

	if networkLinkRegex.MatchString(old.(string)) {
		parts := networkLinkRegex.FindStringSubmatch(old.(string))
		i, err := strconv.ParseInt(parts[1], 10, 64)
		if err == nil {
			if project.ProjectNumber == i {
				if err := diff.SetNew("network", old); err != nil {
					return err
				}
				return nil
			}
		}
	}
	return nil
}
